package cn.touna.jss.modules.third.baihang.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.Header;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import sun.misc.BASE64Decoder;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;

public class ReportService {

    public String makePostCall(RequestParameter requestParameter, String url, String type) throws Exception{
        HttpPost httpPost = new HttpPost(url);
        String queryString = requestParameter.toString();
        queryString = requestParameter.toEncryptString("3DES");
        StringEntity entity = new StringEntity(queryString, "UTF-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        CloseableHttpClient httpClient;
        if ("https".equals(type)) {
            httpClient = MyHttpsClient.createSSLInsecureClient();
        }else{
            httpClient = HttpClients.createDefault();
        }
        CloseableHttpResponse resp = httpClient.execute(httpPost);

        return EntityUtils.toString(resp.getEntity(), "utf-8");
    }


    public String post(String url,  String type,JSONObject requestParameter,String authorization) throws Exception{
        HttpPost httpPost = new HttpPost(url);
        String queryString = requestParameter.toString();
        StringEntity entity = new StringEntity(queryString, "UTF-8");
        entity.setContentType("application/json");
        Header header =  new BasicHeader("Authorization",authorization);
        httpPost.setHeader(header);
        httpPost.setEntity(entity);

        CloseableHttpClient httpClient;
        if ("https".equals(type)) {
            httpClient = MyHttpsClient.createSSLInsecureClient();
        }else{
            httpClient = HttpClients.createDefault();
        }
        CloseableHttpResponse resp = httpClient.execute(httpPost);

        return EntityUtils.toString(resp.getEntity(), "utf-8");
    }


    public String decryptResponse(String response, String keyType, String key) throws Exception{

        if (keyType.toLowerCase().equals("3des") || keyType.toLowerCase().equals("desede") ){
            DESedeKeySpec dks = new DESedeKeySpec(new BASE64Decoder().decodeBuffer(key));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
            SecretKey secretKey = keyFactory.generateSecret(dks);

            Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            byte[] b = cipher.doFinal(new BASE64Decoder().decodeBuffer(response));
            String param = new String(b, "utf-8");

            return param;
        }
        return null;
    }
}
